<!doctype html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Redis," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.1" />






<meta name="description" content="1.string 常用操作123456789set key1 yanyiget key1  set key1 yanyi1           # 一个key对应一个value，多次赋值，会覆盖前面的valuesetnx key2 aaa            # 返回1get key2setnx key2 bbb            # 返回0;说明：setnx 如果key存在，则返回0，不存">
<meta name="keywords" content="Redis">
<meta property="og:type" content="article">
<meta property="og:title" content="Redis 常用命令">
<meta property="og:url" content="https://hcldirgit.github.io/2017/09/03/Redis/4. Redis 常用命令/index.html">
<meta property="og:site_name" content="失落的乐章">
<meta property="og:description" content="1.string 常用操作123456789set key1 yanyiget key1  set key1 yanyi1           # 一个key对应一个value，多次赋值，会覆盖前面的valuesetnx key2 aaa            # 返回1get key2setnx key2 bbb            # 返回0;说明：setnx 如果key存在，则返回0，不存">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2017-09-02T17:30:49.491Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Redis 常用命令">
<meta name="twitter:description" content="1.string 常用操作123456789set key1 yanyiget key1  set key1 yanyi1           # 一个key对应一个value，多次赋值，会覆盖前面的valuesetnx key2 aaa            # 返回1get key2setnx key2 bbb            # 返回0;说明：setnx 如果key存在，则返回0，不存">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://hcldirgit.github.io/2017/09/03/Redis/4. Redis 常用命令/"/>





  <title>Redis 常用命令 | 失落的乐章</title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  




<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
  ga('create', '85*****1', 'auto');
  ga('send', 'pageview');
</script>


  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?87980c**************99ec5e26fb5";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>











  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">失落的乐章</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">技术面前，永远都是学生。</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-message">
          <a href="/message" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-external-link"></i> <br />
            
            留言
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://hcldirgit.github.io/2017/09/03/Redis/4. Redis 常用命令/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="失落的乐章">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/0.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="失落的乐章">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Redis 常用命令</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-09-03T02:09:22+08:00">
                2017-09-03
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="1-string-常用操作"><a href="#1-string-常用操作" class="headerlink" title="1.string 常用操作"></a>1.string 常用操作</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="built_in">set</span> key1 yanyi</div><div class="line">get key1  </div><div class="line"><span class="built_in">set</span> key1 yanyi1           <span class="comment"># 一个key对应一个value，多次赋值，会覆盖前面的value</span></div><div class="line">setnx key2 aaa            <span class="comment"># 返回1</span></div><div class="line">get key2</div><div class="line">setnx key2 bbb            <span class="comment"># 返回0;说明：setnx 如果key存在，则返回0，不存在会直接创建这个key</span></div><div class="line">setex key3 10 1           <span class="comment"># 这个是用来给key设定过期时间的</span></div><div class="line">mset key1 1 key2 2 key3 3 <span class="comment"># 同时设置多个key</span></div><div class="line">mget key1 key2 key3</div></pre></td></tr></table></figure>
<h2 id="2-hash-数据常用操作"><a href="#2-hash-数据常用操作" class="headerlink" title="2.hash 数据常用操作"></a>2.hash 数据常用操作</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">hset user1  name aming               <span class="comment"># 建立hash</span></div><div class="line">hset user1 age 30</div><div class="line">hset user1 job  it</div><div class="line">hgetall user1</div><div class="line">hmset user2 name yanyi age 30 job it <span class="comment"># 批量建立键值对</span></div><div class="line">hmget user2</div><div class="line">hmget user2 name age  job</div><div class="line">hdel user2 job                       <span class="comment"># 删除指定filed</span></div><div class="line">hkeys user2                          <span class="comment"># 打印所有的key</span></div><div class="line">hvals user2                          <span class="comment"># 打印所有的values</span></div><div class="line">hlen user2                           <span class="comment"># 查看hash有几个filed</span></div></pre></td></tr></table></figure>
<h2 id="3-list-数据常用操作"><a href="#3-list-数据常用操作" class="headerlink" title="3.list 数据常用操作"></a>3.list 数据常用操作</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">lpush lista  a                 <span class="comment"># 从左侧压入一个元素</span></div><div class="line">lpush lista b</div><div class="line">lrange lista  0 -1             <span class="comment"># 0 为头，-1为尾</span></div><div class="line">lpop lista                     <span class="comment"># 从左侧取出</span></div><div class="line">rpush lista  1                 <span class="comment"># 从右侧压入一个元素</span></div><div class="line">rpush lista  2</div><div class="line">lrange lista  0 -1</div><div class="line">rpop lista                     <span class="comment"># 从右侧取第一个元素</span></div><div class="line">linsert  lista  before  2 3    <span class="comment"># 在2的前面插入一个元素为3</span></div><div class="line">lset lista 4 bbb               <span class="comment"># 把第5个元素修改为bbb</span></div><div class="line">lindex lista 0                 <span class="comment"># 查看第1个元素</span></div><div class="line">lindex lista 3                 <span class="comment"># 查看第4个元素</span></div><div class="line">llen lista                     <span class="comment"># 查看链表中有几个元素</span></div></pre></td></tr></table></figure>
<h2 id="4-set-数据常用操作"><a href="#4-set-数据常用操作" class="headerlink" title="4.set 数据常用操作"></a>4.set 数据常用操作</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">sadd seta aaa               <span class="comment">#向集合seta中放入元素</span></div><div class="line">smembers seta               <span class="comment"># 查看集合中的所有元素</span></div><div class="line">srem  seta    aaa           <span class="comment"># 删除元素</span></div><div class="line">spop  seta                  <span class="comment"># 随机取出一个元素，删除</span></div><div class="line">sdiff  seta  setb           <span class="comment"># 求差集，以seta为标准</span></div><div class="line">sdiffstore setc seta setb   <span class="comment"># 求差集并且存储，存储到了setc里</span></div><div class="line">sinter seta setb            <span class="comment"># 求交集</span></div><div class="line">sinterstore  setd seta setb <span class="comment"># 将交集存储setd</span></div><div class="line">sunion seta setb            <span class="comment"># 求并集</span></div><div class="line">sunionstore sete seta setb  <span class="comment"># 求并集并存储到sete</span></div><div class="line">sismember seta aaa          <span class="comment"># 判断一个元素是否属于一个集合</span></div><div class="line">srandmember  seta           <span class="comment"># 随机取出一个元素，但不删除</span></div></pre></td></tr></table></figure>
<h2 id="5-zset-数据常用操作"><a href="#5-zset-数据常用操作" class="headerlink" title="5.zset 数据常用操作"></a>5.zset 数据常用操作</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">zadd zseta 11 123             <span class="comment"># 创建有序集合</span></div><div class="line">zrange zseta 0 -1             <span class="comment"># 显示所有元素，按顺序显示</span></div><div class="line">zrange zseta 0 -1 withscores  <span class="comment"># 可以带上分值</span></div><div class="line">zrem zseta 222                <span class="comment"># 删除指定元素</span></div><div class="line">zrank zseta  222              <span class="comment"># 返回元素的索引值，索引值从0开始，按score正向排序</span></div><div class="line">zrevrank zseta 222            <span class="comment"># 同上，不同的是，按score反序排序</span></div><div class="line">zrevrange  zseta  0 -1        <span class="comment"># 反序显示所有元素，并带分值</span></div><div class="line">zcard zseta                   <span class="comment"># 返回集合中所有元素的个数</span></div><div class="line">zcount  zseta 1 10            <span class="comment"># 返回分值范围1-10的元素个数</span></div><div class="line">zrangebyscore  zseta 1 10     <span class="comment"># 返回分值范围1-10的元素</span></div><div class="line">zremrangebyrank zseta  0 2    <span class="comment"># 删除索引范围0-2的元素，按score正向排序</span></div><div class="line">zremrangebyscore zseta  1 10  <span class="comment"># 删除分值范围1-10的元素</span></div></pre></td></tr></table></figure>
<h2 id="6-键值相关操作"><a href="#6-键值相关操作" class="headerlink" title="6.键值相关操作"></a>6.键值相关操作</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">keys *                 <span class="comment"># 取出所有key</span></div><div class="line">keys my*               <span class="comment"># 模糊匹配</span></div><div class="line">exists name            <span class="comment"># 有name键 返回1 ，否则返回0；</span></div><div class="line">del  key1              <span class="comment"># 删除一个key，成功返回1 ，否则返回0；</span></div><div class="line">EXPIRE key1 100        <span class="comment"># 设置key1 100s后过期</span></div><div class="line">ttl key                <span class="comment"># 查看键 还有多长时间过期，单位是s,当 key 不存在时，返回 -2 。 当 key 存在但没有设置剩余生存时间时，返回 -1 。 否则，返回 key 的剩余生存时间。</span></div><div class="line">select  0              <span class="comment"># 代表选择当前数据库，默认进入0 数据库</span></div><div class="line">move age 1             <span class="comment"># 把age 移动到1 数据库</span></div><div class="line">persist key1           <span class="comment"># 取消key1的过期时间</span></div><div class="line">randomkey              <span class="comment"># 随机返回一个key</span></div><div class="line">rename oldname newname <span class="comment"># 重命名key</span></div><div class="line"><span class="built_in">type</span> key1              <span class="comment"># 返回键的类型</span></div></pre></td></tr></table></figure>
<h2 id="7-服务相关操作"><a href="#7-服务相关操作" class="headerlink" title="7.服务相关操作"></a>7.服务相关操作</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">dbsize   <span class="comment"># 返回当前数据库中key的数目</span></div><div class="line">info     <span class="comment"># 返回redis数据库状态信息</span></div><div class="line">flushdb  <span class="comment"># 清空当前数据库中所有的键</span></div><div class="line">flushall <span class="comment"># 清空所有数据库中的所有的key</span></div></pre></td></tr></table></figure>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Redis/" rel="tag"># Redis</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/09/03/Redis/3. Redis主从配置/" rel="next" title="Redis主从配置">
                <i class="fa fa-chevron-left"></i> Redis主从配置
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/09/03/Redis/5. php 使用 redis 储存/" rel="prev" title="php 使用 redis 储存">
                php 使用 redis 储存 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/0.png"
               alt="失落的乐章" />
          <p class="site-author-name" itemprop="name">失落的乐章</p>
           
              <p class="site-description motion-element" itemprop="description">失落的乐章的Blog</p>
          
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/">
                <span class="site-state-item-count">627</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">38</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/hcldirgit" target="_blank" title="GitHub">
                  
                    <i class="fa fa-fw fa-github"></i>
                  
                  GitHub
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-string-常用操作"><span class="nav-number">1.</span> <span class="nav-text">1.string 常用操作</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-hash-数据常用操作"><span class="nav-number">2.</span> <span class="nav-text">2.hash 数据常用操作</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-list-数据常用操作"><span class="nav-number">3.</span> <span class="nav-text">3.list 数据常用操作</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-set-数据常用操作"><span class="nav-number">4.</span> <span class="nav-text">4.set 数据常用操作</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-zset-数据常用操作"><span class="nav-number">5.</span> <span class="nav-text">5.zset 数据常用操作</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-键值相关操作"><span class="nav-number">6.</span> <span class="nav-text">6.键值相关操作</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#7-服务相关操作"><span class="nav-number">7.</span> <span class="nav-text">7.服务相关操作</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">失落的乐章</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Muse
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.1"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.1"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.1"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.1"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.1"></script>



  


  




	





  





  





  






  





  

  

  

  

  

  

</body>
</html>
